<template>
  <div class="aside-default">
    <div v-if="selected.assetId" class="tab-title">
      <icon-svg name="iconReturn" @click.native="prev"></icon-svg>
      {{ selected.assetNm }}
    </div>
    <el-input v-model="assetNm" placeholder="输入搜索关键字" @keyup.native.enter="search">
      <i class="el-icon-close" slot="suffix" @click="clearSearch" v-if="assetNm.length"></i>
      <el-button slot="append" @click="getList"><icon-svg name="iconSearch"></icon-svg></el-button>
    </el-input>
    <template v-if="list.length">
      <ul>
        <li
          v-for="item in list"
          :key="item.assetId"
          :draggable="item.assetType == 1"
          @dragstart="copyDrag($event, item)"
          @click="getList(item)"
          class="drag-item"
          :class="{
            'tag-item': item.assetType === 1
          }"
        >
          <div class="asset-name">{{ item.assetNm }}</div>
          <i class="el-icon-arrow-right" v-if="item.assetType !== 1"></i>
          <div class="list-handler" v-if="item.assetType === 1">
            <el-tooltip effect="dark" content="添加" placement="top" :enterable="false">
              <span class="plus" @click.prevent="$emit('addItem', item)"><i class="el-icon-plus"></i></span>
            </el-tooltip>
            <el-tooltip effect="dark" placement="top" :enterable="false">
              <span slot="content">
                口径：{{ item.bizStmt }}
                <template v-if="item.plfmDataDate">
                  <br />
                  <br />
                  数据日期: {{ item.plfmDataDate }}
                </template>
              </span>
              <icon-svg name="iconPreview" @click.native.prevent="gotoDetail(item)"></icon-svg>
            </el-tooltip>
          </div>
        </li>
      </ul>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="params.currentPageNum"
        :page-size="params.pageSize"
        :pager-count="5"
        background
        layout="total, prev, pager, next"
        small
        :total="totalSize"
      >
      </el-pagination>
    </template>
    <p v-else>{{ $parent.$parent.$parent.loading ? '' : '暂无内容' }}</p>
  </div>
</template>

<script>
export default {
  props: {
    listType: {
      type: Number,
      required: true,
      default: 0
    }
  },
  data() {
    return {
      list: [],
      assetNm: '',
      selected: {},
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.params.currentPageNum = val
      this.getList(this.selected)
    },
    getList(item = {}) {
      if (item && item.assetType === 1) return
      this.params.queryParams.listType = this.listType
      this.params.queryParams.assetNm = this.assetNm
      this.params.queryParams.assetType = item.assetType || (this.selected.assetType ? this.selected.assetType : 1)
      if (item.assetType === 3 || (this.selected.assetType && this.selected.assetType === 3)) {
        this.params.queryParams.assetId = item.assetId || this.selected.assetId
      }
      this.$emit('toggleLoading', true)
      this.$ajax({
        url: this.$ajax.setUrl('tagFactory/pageList'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.list = data.data.records
          this.totalSize = data.data.totalSize
          if (item.assetType === 3) this.selected = item
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.$emit('toggleLoading', false)
      })
    },
    search() {
      this.params = {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      }
      this.getList()
    },
    clearSearch() {
      this.assetNm = ''
      this.getList()
    },
    gotoDetail(item) {
      this.$blank('market-tag', { params: { id: item.assetId }, newpage: true })
    },
    copyDrag(event, item) {
      this.$emit('drag', event, item)
    },
    prev() {
      this.assetNm = ''
      this.selected = {}
      this.params = {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      }
      this.getList()
    }
  }
}
</script>

<style lang="scss"></style>
